﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Runtime.ExceptionServices;
using System.Security;
using System.IO;
using System.Threading;



namespace LaneLpnr.libvlc
{
    using libvlc_media_t = System.IntPtr;
    using libvlc_media_player_t = System.IntPtr;
    using libvlc_instance_t = System.IntPtr;


    public struct libvlc_media_stats_t
    {
        /* Input */
        public int i_read_bytes;
        public float f_input_bitrate;

        /* Demux */
        public int i_demux_read_bytes;
        public float f_demux_bitrate;
        public int i_demux_corrupted;
        public int i_demux_discontinuity;

        /* Decoders */
        public int i_decoded_video;
        public int i_decoded_audio;

        /* Video Output */
        public int i_displayed_pictures;
        public int i_lost_pictures;

        /* Audio output */
        public int i_played_abuffers;
        public int i_lost_abuffers;

        /* Stream output */
        public int i_sent_packets;
        public int i_sent_bytes;
        public float f_send_bitrate;
    }

    public class SafeNativeMethods
    {
        // 创建一个libvlc实例，它是引用计数的  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern libvlc_instance_t libvlc_new(int argc, IntPtr argv);

        // 释放libvlc实例  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void libvlc_release(libvlc_instance_t libvlc_instance);

        //获取libvlc的版本  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern String libvlc_get_version();

        //从视频来源(例如http、rtsp)构建一个libvlc_meida  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern libvlc_media_t libvlc_media_new_location(libvlc_instance_t libvlc_instance, IntPtr path);

        //从本地文件路径构建一个libvlc_media  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern libvlc_media_t libvlc_media_new_path(libvlc_instance_t libvlc_instance, IntPtr path);

        //释放libvlc_media  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void libvlc_media_release(libvlc_media_t libvlc_media_inst);

        // 创建一个空的播放器  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern libvlc_media_player_t libvlc_media_player_new(libvlc_instance_t libvlc_instance);

        //从libvlc_media构建播放器  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern libvlc_media_player_t libvlc_media_player_new_from_media(libvlc_media_t libvlc_media);

        //释放播放器资源  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void libvlc_media_player_release(libvlc_media_player_t libvlc_mediaplayer);

        // 将视频(libvlc_media)绑定到播放器上  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void libvlc_media_player_set_media(libvlc_media_player_t libvlc_media_player, libvlc_media_t libvlc_media);

        // 设置图像输出的窗口  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void libvlc_media_player_set_hwnd(libvlc_media_player_t libvlc_mediaplayer, Int32 drawable);

        //播放器播放  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern int libvlc_media_player_play(libvlc_media_player_t libvlc_mediaplayer);

        //播放器暂停  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void libvlc_media_player_pause(libvlc_media_player_t libvlc_mediaplayer);

        //播放器停止  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void libvlc_media_player_stop(libvlc_media_player_t libvlc_mediaplayer);

        // 解析视频资源的媒体信息(如时长等)  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void libvlc_media_parse(libvlc_media_t libvlc_media);

        // 返回视频的时长(必须先调用libvlc_media_parse之后，该函数才会生效)  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern Int64 libvlc_media_get_duration(libvlc_media_t libvlc_media);

        // 当前播放时间  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern Int64 libvlc_media_player_get_time(libvlc_media_player_t libvlc_mediaplayer);

        // 设置播放时间  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void libvlc_media_player_set_time(libvlc_media_player_t libvlc_mediaplayer, Int64 time);

        // 获取音量  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern int libvlc_audio_get_volume(libvlc_media_player_t libvlc_media_player);

        //设置音量  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void libvlc_audio_set_volume(libvlc_media_player_t libvlc_media_player, int volume);

        // 设置全屏  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void libvlc_set_fullscreen(libvlc_media_player_t libvlc_media_player, int isFullScreen);

        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern int libvlc_get_fullscreen(libvlc_media_player_t libvlc_media_player);

        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void libvlc_toggle_fullscreen(libvlc_media_player_t libvlc_media_player);

        //判断播放时是否在播放  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern bool libvlc_media_player_is_playing(libvlc_media_player_t libvlc_media_player);

        //判断播放时是否能够Seek  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern bool libvlc_media_player_is_seekable(libvlc_media_player_t libvlc_media_player);

        //判断播放时是否能够Pause  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern bool libvlc_media_player_can_pause(libvlc_media_player_t libvlc_media_player);

        //判断播放器是否可以播放  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern int libvlc_media_player_will_play(libvlc_media_player_t libvlc_media_player);

        //进行快照  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern int libvlc_video_take_snapshot(libvlc_media_player_t libvlc_media_player, int num, char[] filepath, int i_width, int i_height);

        //获取Media信息  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern libvlc_media_t libvlc_media_player_get_media(libvlc_media_player_t libvlc_media_player);

        //获取媒体信息  
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern int libvlc_media_get_stats(libvlc_media_t libvlc_media, ref libvlc_media_stats_t lib_vlc_media_stats);
        [DllImport(@"libvlc.DLL", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void libvlc_video_set_aspect_ratio(libvlc_media_player_t libvlc_media_player, char[] aspect);



    }
}
